var models = require('../mysql/mysql');
// /**
//  * 所有消息
//  *  get
//  *   url: /message
//  */
const getAllMessage = async (req, res, next) => {
    // 接收参数
    var sql = `select * from message`;
    let data = await models(sql)

    if (data.length > 0) {
        res.send({
            code: 1,
            info:data
        })
    } else {
        res.send({
            code: -1,
            info: '日志列表为空!',
        })
    }
}
// /**
//  * 对应操作员消息状态
//  *  get
//  *   url: /message/state
//  */
const getMessageState = async (req, res, next) => {
    // 接收参数
    const {user} = req.query
    var sql = `select * from operator_message where user='${user}'`;
    let data = await models(sql)

    if (data) {
        res.send({
            code: 1,
            info:data
        })
    } else {
        res.send({
            code: -1,
            info: '请求失败 或 日志列表为空!',
        })
    }
}
// /**
//  * 添加所有消息
//  *  post
//  *   url: /message/update
//  */
const getUpdateMessage = async (req, res, next) => {
	// 接收参数
	const {message,title,category,user,to_user} = req.body
    console.log(req.body);
    console.log('message>>>',message);
    var sql = `insert into message(message,title,category,user,to_user) 
	values('${message}',"${title}","${category}","${user}","${to_user}")`;
	// values("${'测试内容'}","${'测试标题'}","${'个人消息'}","${'root'}","${'jiang'}")`;

	try {
		let data = await models(sql)
		res.send({
			code: 1,
			info: '添加成功',
            datainfo:data
		})
	} catch (eror) {
		res.send({
			code: -1,
			info: '添加失败',
		})
	}
}
// /**
//  * 删除消息
//  *  post
//  *   url: /message/delete
//  */
const delteMsgById = async (req, res, next) => {
	// 接收参数
	console.log(req.body);
	const {
		id
	} = req.body
	var sql = `delete from message where id="${id}"`;

	try {
		await models(sql)
		res.send({
			code: 1,
			info: '删除成功',
		})
	} catch (eror) {
		res.send({
			code: -1,
			info: '删除失败',
		})
	}
}
// /**
//  * 添加操作者消息 
//  *  post
//  *   url: /message/update_operator
//  */
const getUpdateOperatorMessage = async (req, res, next) => {
	// 接收参数
	const {userMessage,user} = req.body
    // console.log(req.boy);
    // console.log(userMessage);
    // console.log(user);
	// var sql = `update operator_message set message='${JSON.stringify([ { id: 1, state: 1 }, { id: 2, state: 0 }, { id: 4, state: 0 } ])}' where user="${'jiang'}";`
	var sql = `update operator_message set message='${JSON.stringify(userMessage)}' where user="${user}";`

	try {
		await models(sql)
		res.send({
			code: 1,
			info: '修改成功',
		})
	} catch (eror) {
		res.send({
			code: -1,
			info: '修改失败',
		})
	}
}
// /**
//  * (修改操作员信息状态)
//  *  post
//  *   url: /message/update_operator_byid
//  */
const getUpdateOperatorMessageById = async (req, res, next) => {
	// 接收参数
	const {userMessage,id} = req.body
    // console.log(req.boy);
    // console.log(userMessage);
    // console.log(id);
	// var sql = `update operator_message set message='${JSON.stringify([ { id: 1, state: 1 }, { id: 2, state: 0 }, { id: 4, state: 0 } ])}' where user="${'jiang'}";`
	var sql = `update operator_message set message='${JSON.stringify(userMessage)}' where id="${id}";`

	try {
		await models(sql)
		res.send({
			code: 1,
			info: '修改成功',
		})
	} catch (eror) {
		res.send({
			code: -1,
			info: '修改失败',
		})
	}
}

module.exports = {getAllMessage,getMessageState,getUpdateMessage,getUpdateOperatorMessage,getUpdateOperatorMessageById,delteMsgById}